//
//  IndexLeftView.m
//  WRY
//
//  Created by zhong sckj on 10-10-27.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "IndexLeftView.h"



@implementation IndexLeftView
@synthesize listData;
@synthesize indexViewVc;

- (id)initWithFrame:(CGRect)frame initArray:(NSMutableArray *)dataArray {
    if ((self = [super initWithFrame:frame])) {
		self.listData=dataArray;
		[self drawSector];
    }
    return self;
}
-(void)drawSector
{
	pieChartController = [[SC_PieChartVC alloc] init];
	[pieChartController setViewFrame:CGRectMake(0.0, 0.0,self.frame.size.width,self.frame.size.height)];
	[self addSubview:pieChartController.view];
	[(SC_PieView *)pieChartController.view setDelegate:self];
	
	SC_PieChartData *data = [[SC_PieChartData alloc] init];
	NSMutableArray *dataArray = [[NSMutableArray alloc] init];
	
	//废水污染源
	NSString *feishui=@"";
	//废弃污染源
	NSString *feiqi=@"";
	//污水厂
	NSString *wushuichang=@"";
	
	for (int i=0; i<[listData count]; i++) {
		NSString *enterpriseType=[(NSDictionary *)[listData objectAtIndex:i] objectForKey:@"DetailTypeCode"];
		NSDictionary *direct=(NSDictionary *)[listData objectAtIndex:i];
		if ([enterpriseType isEqualToString:@"01"]) {
			//废水
			feishui=[direct objectForKey:@"EnterpriseCount"];
		}
		else if([enterpriseType isEqualToString:@"02"])
		{
			feiqi=[direct objectForKey:@"EnterpriseCount"];
		}
		else if([enterpriseType isEqualToString:@"03"])
		{
			wushuichang=[direct objectForKey:@"EnterpriseCount"];
		}
		
	}
	
	
	data.value = [feishui intValue];
	data.title = @"废水污染源";
	//data.color = [UIColor greenColor];
	[dataArray addObject:data];
	data = [[SC_PieChartData alloc] init];
	data.value = [feiqi intValue];
	data.title = @"废气污染源";
	//data.color = [UIColor orangeColor];
	[dataArray addObject:data];
	data = [[SC_PieChartData alloc] init];
	data.value = [wushuichang intValue];
	data.title = @"污水处理厂";
	//data.color = [UIColor blueColor];
	[dataArray addObject:data];
	[data release];
	[pieChartController setData:dataArray];
	[dataArray release];
	//[pieChartController release];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark SC_PieViewDelegate
- (void)needReloadMarkersFor:(NSArray *)dataArray Dots:(NSArray *)dotArray {
	//NSArray *array = [cv getDotArray];
	for (UIView *view in pieChartController.view.subviews) {
		[view removeFromSuperview];
	}
	float total = 0.0f;
	for (SC_PieChartData * data in dataArray) {
		total += data.value;
	}
	for (int i = 0; i < [dotArray count]; i ++) {
		SC_PieChartData *data = [dataArray objectAtIndex:i];
		SC_PieChartMarkerView *marker = [[SC_PieChartMarkerView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 182.0f, 82.0f)];
		[pieChartController.view addSubview:marker];
		[marker.button setImage:[UIImage imageNamed:@"btn29x29.png"] forState:UIControlStateNormal];
		marker.button.tag = i+1;
		[marker.button addTarget:self action:@selector(markerBtPressed:) forControlEvents:UIControlEventTouchUpInside];
		marker.titleLabel.text = data.title;
		marker.countLabel.text = [NSString stringWithFormat:@"%.0f个(%.0f%%)",data.value,data.value/total * 100];
		//marker.overLabel.text = [NSString stringWithFormat:@"超标"];
		if (data.overValue > 0.0f) {
			marker.overLabel.text = [NSString stringWithFormat:@"超标：%.0ld",(long)data.overValue];
		}
		NSDictionary *dict = [dotArray objectAtIndex:i];
		float x = [[dict objectForKey:@"x"] floatValue];
		float y = [[dict objectForKey:@"y"] floatValue];
		CGSize size = pieChartController.view.bounds.size;
		marker.center = CGPointMake(x,y);
		NSString *imageName;
		NSString *direction;
		if (x < size.width / 2.0f && y < size.height / 2.0f) {
			imageName = @"right182x82.png";
			direction = @"DownRight";
		}
		else if (x < size.width / 2.0f && y > size.height / 2.0f) {
			imageName = @"right182x82_2.png";
			direction = @"UpRight";
		}
		else if (x > size.width / 2.0f && y < size.height / 2.0f) {
			imageName = @"left182x82.png";
			direction = @"DownLeft";
		}
		else if (x > size.width / 2.0f && y > size.height / 2.0f) {
			imageName = @"left182x82_2.png";
			direction = @"UpLeft";
		}
		[marker.markerImage setImage:[UIImage imageNamed:imageName]];
		[marker setDirectionString:direction];
		[marker setNeedsDisplay];
		[marker release];
	}
	
}
- (void)markerBtPressed:(id)sender {
	
	[indexViewVc eventDetailAction:sender];
}
- (void)dealloc {
	[pieChartController release];
	[indexViewVc release];
	[listData release];
    [super dealloc];
}


@end
